package com.lyj.service.impl;

import com.lyj.bean.Ad;
import com.lyj.dao.AdDao;
import com.lyj.dto.AdDto;
import com.lyj.service.AdService;
import com.lyj.util.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyj on 2017/10/10.
 */
@Service
public class AdServiceImpl implements AdService {

    @Autowired
    private AdDao adDao;

    @Value("${adImage.savePath}")
    private  String adImageSavePath;

    @Value("${adImage.url}")
    private String adImageUrl;

    /**
     * 文件上传
     * @param adDto
     * @return
     */
    public boolean add(AdDto adDto) {
        Ad ad=new Ad();
        ad.setTitle(adDto.getTitle());
        ad.setLink(adDto.getLink());
        ad.setWeight(adDto.getWeight());
        if(adDto.getImgFile()!= null && adDto.getImgFile().getSize()>0){
            String fileName=System.currentTimeMillis()+"_"+adDto.getImgFile().getOriginalFilename();
            File file =new File(adImageSavePath + fileName);
            File fileFolder =new File(adImageSavePath);
            if(!fileFolder.exists()){
                fileFolder.mkdirs();
            }
            try {
                adDto.getImgFile().transferTo(file);
                ad.setImgFileName(fileName);
                adDao.insert(ad);
                return  true;
            } catch (IOException e) {
                // todo 需要添加日志
                //e.printStackTrace();
                return  false;
            }
        }else {
            return  false;
        }
    }

    /**
     * 根据主键获取广告的Dto对象
     * @param id 广告表主键值
     * @return
     */
    public AdDto getById(Long id) {
        AdDto result=new AdDto();
        Ad ad=adDao.selectById(id);
        BeanUtils.copyProperties(ad,result);
        result.setImg(adImageUrl+ad.getImgFileName());
        return result;
    }

    /**
     *  修改广告操作
     * @param adDto
     * @return
     */
    public boolean modify(AdDto adDto) {

        return false;
    }

    /**
     *  分页搜索广告列表
     * @param adDto 查询条件（包含分页对象）
     * @return
     */
    public List<AdDto> searchByPage(AdDto adDto) {
        List<AdDto> result = new ArrayList<AdDto>();
        Ad condition = new Ad();
        BeanUtils.copyProperties(adDto, condition);
        List<Ad> adList = adDao.selectByPage(condition);
        for (Ad ad : adList) {
            AdDto adDtoTemp = new AdDto();
            adDtoTemp.setImg(adImageUrl + ad.getImgFileName());
            BeanUtils.copyProperties(ad, adDtoTemp);
            result.add(adDtoTemp);
        }
        return result;
    }

    /**
     * 根据id删除广告
     * @param id
     * @return
     */
    public Integer remove(Long id){
        Ad ad = adDao.selectById(id);
        int deleteRows=0;
        if(deleteRows==1) {
            deleteRows=adDao.delete(id);
            FileUtil.delete(adImageSavePath + ad.getImgFileName());
        }
        return deleteRows;
    }









}
